package com.intexh.speedandroid.helper;

import android.content.Context;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.intexh.speedandroid.utils.FileUtils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public enum PickerHelper {
    INSTANCE;

    public void init(Context context){
        //初始化城市数据
        initCityData(context);
    }

    private List<ProvinceBean> options1Items = new ArrayList<>();
    private List<ArrayList<ProvinceBean.CityBean>> options2Items = new ArrayList<>();
    private List<ArrayList<ArrayList<ProvinceBean.CityBean.AreaBean>>> options3Items = new ArrayList<>();

    private void initCityData(Context context) {
        String JsonData = FileUtils.getJson(context, "citys2.json");
        List<ProvinceBean> provinceBeanList = parseData(JsonData);//用Gson 转成实体
        options1Items = provinceBeanList;
        for (int i = 0; i < provinceBeanList.size(); i++) {//遍历省份
            ArrayList<ProvinceBean.CityBean> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<ProvinceBean.CityBean.AreaBean>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c = 0; c < provinceBeanList.get(i).getCity().size(); c++) {//遍历该省份的所有城市
                ProvinceBean.CityBean CityName = provinceBeanList.get(i).getCity().get(c);
                CityList.add(CityName);//添加城市
                ArrayList<ProvinceBean.CityBean.AreaBean> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (provinceBeanList.get(i).getCity().get(c).getArea() == null
                        || provinceBeanList.get(i).getCity().get(c).getArea().size() == 0) {
                    City_AreaList.add(null);
                } else {
                    City_AreaList.addAll(provinceBeanList.get(i).getCity().get(c).getArea());
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            options3Items.add(Province_AreaList);
        }
    }

    public List<ProvinceBean> parseData(String result) {//Gson 解析
        List<ProvinceBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                ProvinceBean entity = gson.fromJson(data.optJSONObject(i).toString(), ProvinceBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }

    OptionsPickerView pvOptions;
    public void showCityPicker(Context context,CitySelectListener listener){
        if(pvOptions==null){
            pvOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    ProvinceBean provinceBean = options1Items.get(options1);
                    ProvinceBean.CityBean cityBean = options2Items.get(options1).get(options2);
                    ProvinceBean.CityBean.AreaBean areaBean = options3Items.get(options1).get(options2).get(options3);
                    listener.onCitySelect(provinceBean,cityBean,areaBean);
                }
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                //返回的分别是三个级别的选中位置
//                String tx = options1Items.get(options1).getPickerViewText() +
//                        options2Items.get(options1).get(options2) +
//                        options3Items.get(options1).get(options2).get(options3);
//            }
            })

                    .setTitleText("城市选择")
//                .setDividerColor(Color.BLACK)
//                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                    .setContentTextSize(20)
                    .build();

        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
            pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        }
        pvOptions.show();
    }


    public interface CitySelectListener{
       void onCitySelect(ProvinceBean province, ProvinceBean.CityBean cityBean, ProvinceBean.CityBean.AreaBean areaBean);
    }

}
